package Offer;

import java.util.*;

/**
 * 从上到下打印二叉树
 * @author 是阿秋啊
 * @date 2022/04/04 20:01
 **/
public class Num32 {
    public int[] levelOrder(TreeNode root) {
        if (root == null) {
            return new int[0];
        }
        ArrayList<Integer> ret = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            ret.add(node.val);
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
        int[] arr = new int[ret.size()];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = ret.get(i);
        }
        return arr;
    }
}
